local tf = 1 --tigers fury
local sr = 1 --savage roar
local dc = 1 --dream of cenarius
local protect = 0
local rip_protect = 0

local o
local me, spec
local timer, throttle		= 0, 0.1
local dot_damage, targets	= AffDots.dot_damage, AffDots.targets

function onUpdate(self,elapsed)
    timer = timer + elapsed;
    if timer >= throttle then
		for k,v in pairs(AffDots.track) do
			AffDots.track[k].update(AffDots.track[k])
			AffDots:FrameDraw(AffDots.track[k])
		end
        timer = 0
    end
end

local function Round(num) return math.floor(num+.5) end
local function SecondsRound(num)
	if num > 2 then return math.floor(num+.5)
	else return math.floor(num*10+.5)/10 end
end

function AffDots:FindColor(GUID,time_left,spell,pandemic)
	if not targets[GUID..spell] or not dot_damage[spell] then
		return 7
	end
	local current 		= dot_damage[spell][1]
	local on_target 	= targets[GUID..spell][1]
	if		on_target  < current then color = 1
	elseif 	on_target == current then color = 2
	elseif	on_target  > current then color = 6
	end
	return color
end

AffDots.Update = {
	dot	= function(f)
		local guid = UnitGUID(f.target)
		local _,_,_,_,_,duration,expires = UnitDebuff(f.target,f.name,f.rank,"player")
		if duration and guid and targets[guid..f.spell] then
			if(math.abs(expires-f.timer)>1) then
				f.datanew.expires = expires
				f.datanew.duration = duration
				f.datanew.setcd = true
				f.timer = expires
			end
			f.datanew.color = o["color"..AffDots:FindColor(guid,expires - GetTime(),f.spell,f.pandemic)]
			f.datanew.tick = targets[guid..f.spell][3]
			f.datanew.t2 = SecondsRound(f.timer-GetTime())
			if o.reltext then
				f.datanew.t1 = Round(dot_damage[f.spell][1]*100/targets[guid..f.spell][1])
			elseif o.layout == "bars" then
				f.datanew.t1 = dot_damage[f.spell][1].."/"..targets[guid..f.spell][1]
			else
				f.datanew.t1 = dot_damage[f.spell][1].."\n"..targets[guid..f.spell][1]
			end
		else
			f.datanew.color = o.color7
			f.datanew.t1 = 0
			f.datanew.t2 = ""
			f.datanew.tick = 0
			f.timer = 0
			f.datanew.hidecd = true
		end
	end,
}

local GetDotDmg = {
	function()
	end,
	function()
		local mastery, crit, ap, cp = GetMastery(), GetCritChance(), UnitAttackPower("player"), GetComboPoints("player", "target")
		if crit > 100 then crit = 100 end
		damage_bonus 		= (1+crit/100)*(1+(mastery*3.1)/100)*sr*tf*dc

		damage				= 5*(99+ap*0.3)*damage_bonus
		dps					= Round(damage/15)
		dot_damage[1822]	= {Round(dps/100)/10, 3}
		
		if cp == 0 then damage = 0 else
			damage = 8*((113+320*cp) + 0.0484*cp*ap)*damage_bonus
		end
		dps					= Round(damage/16)
		dot_damage[1079]	= {Round(dps/100)/10, 2}
		if cp > 0 then rip_protect = dot_damage[1079][1] end
	end,
	function() end,
	function() end,
}

local CombatLog = {
	function(_,_,_,event_type,_,source_GUID,_,_,_,dest_GUID,_,_,_, ...)	end,
	function(_,_,_,event_type,_,source_GUID,_,_,_,dest_GUID,_,_,_, ...)
		if source_GUID ~= me then return end
		spell = select(1, ...)
		if event_type == "SPELL_AURA_APPLIED" then
			if spell == 1079 then
				targets[dest_GUID.."1079"] = {rip_protect,GetTime(),dot_damage[1079][2]}
				rip_protect = 0
			elseif spell == 1822 then
				targets[dest_GUID.."1822"] = {dot_damage[1822][1],GetTime(),dot_damage[1822][2]}
			elseif spell == 5217 then
				tf = 1.15
			elseif spell == 52610 then
				sr = 1.3
			elseif spell == 108381 then
				dc = 1.25
			end
		elseif event_type == "SPELL_AURA_REFRESH" then
			if spell == 1079 and GetTime() - protect > 0.5 then
				targets[dest_GUID.."1079"] = {rip_protect,GetTime(),dot_damage[1079][2]}
				rip_protect = 0
			elseif spell == 1822 and GetTime() - protect > 0.5 then
				targets[dest_GUID.."1822"] = {dot_damage[1822][1],GetTime(),dot_damage[1822][2]}
			end
		elseif event_type == "SPELL_DAMAGE" and (spell == 5221 or spell == 102545 or spell == 33876 or spell == 22568) then
			protect = GetTime()
		elseif event_type == "SPELL_AURA_REMOVED" then
			if spell == 5217 then
				tf = 1
			elseif spell == 52610 then
				sr = 1
			elseif spell == 108381 then
				dc = 1
			end
		end
	end,
	function() end,
	function() end,
}

function AffDots:InitSpec()
	AffDots:ReleaseFrames()
	wipe(AffDots.track)
	if spec == 5 then --1
		AffDots.execute_percent = 20
		AffDots:Track(  8921,	  AffDots.Update.dot, "target",   14,  0, 0,  true)
		AffDots:Track( 93402,	  AffDots.Update.dot, "target",   14,  0, 1,  true)
		if o.focus then
		AffDots:Track(  8921,	  AffDots.Update.dot,  "focus",	  14,  0, 0,  true)
		AffDots:Track( 93402,	  AffDots.Update.dot,  "focus",	  14,  0, 1,  true)
		end
	elseif spec == 2 then
		AffDots.execute_percent = 25
		AffDots:Track( 1079,	  AffDots.Update.dot, "target",   16,  0, 0,  true)
		AffDots:Track( 1822,	  AffDots.Update.dot, "target",   15,  0, 1,  true)
		if o.focus then
		AffDots:Track( 1079,	  AffDots.Update.dot,  "focus",	  16,  0, 0,  true)
		AffDots:Track( 1822,	  AffDots.Update.dot,  "focus",	  15,  0, 1,  true)
		end
	end
	AffDots:InitFrames()
end

function AffDots:InitClass()
	AffDots:RegisterEvent("PLAYER_TALENT_UPDATE", "InitClass")
	me = UnitGUID("player")
	AffDots:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
	AffDots:UnregisterEvent("COMBAT_RATING_UPDATE")
	AffDots:UnregisterEvent("SPELL_POWER_CHANGED")
	AffDots:UnregisterEvent("UNIT_STATS")
	AffDots:UnregisterEvent("PLAYER_DAMAGE_DONE_MODS")
	AffDots:UnregisterEvent("UNIT_ATTACK_POWER")
	AffDots:UnregisterEvent("UNIT_COMBO_POINTS")
	AffDots:UnregisterEvent("PLAYER_REGEN_ENABLED")
	AffDots:UnregisterEvent("PLAYER_REGEN_DISABLED")
	AffDots:UnregisterEvent("PLAYER_FOCUS_CHANGED")
	AffDots:UnregisterEvent("MODIFIER_STATE_CHANGED")
	AffDotsTarget:SetScript("OnUpdate", nil)
	AffDotsTarget:Hide(); AffDotsFocus:Hide(); AffDotsBurst:Hide()
	spec = GetSpecialization() or ""
	if (spec == 2) then
		o = self.db.profile
		AffDots.GetDotDmg = GetDotDmg[spec]
		AffDots.CombatLog = CombatLog[spec]
		AffDots:GetDotDmg()
		AffDots:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED", "CombatLog")
		AffDots:RegisterEvent("COMBAT_RATING_UPDATE", "GetDotDmg")
		AffDots:RegisterEvent("SPELL_POWER_CHANGED", "GetDotDmg")
		AffDots:RegisterEvent("UNIT_STATS", "GetDotDmg")
		AffDots:RegisterEvent("PLAYER_DAMAGE_DONE_MODS", "GetDotDmg")
		AffDots:RegisterEvent("UNIT_ATTACK_POWER", "GetDotDmg")
		AffDots:RegisterEvent("UNIT_COMBO_POINTS", "GetDotDmg")
		AffDots:RegisterEvent("PLAYER_REGEN_ENABLED")
		AffDots:RegisterEvent("PLAYER_REGEN_DISABLED")
		AffDots:RegisterEvent("PLAYER_FOCUS_CHANGED")
		AffDots:RegisterEvent("MODIFIER_STATE_CHANGED")
		AffDotsTarget:SetScript("OnUpdate", onUpdate)
		AffDots:InitSpec()
	end
end

function AffDots:GetSpecOptions(spec)
	if spec == "DRUID2" then
		self.options.args.spec.name = L["Feral"]
		self.options.args.spec.args = {
			desc = {
				order = 1,name = "Test version, no options currently",type = "description",fontSize="medium"
			},
		}
	else
		self.options.args.spec.name = "Specialization"
		self.options.args.spec.args = {
			spacer1 = {
				order = 1,type = "description",name="Specialization not supported, AffDots deactivated", width="full",
			},
		}
	end
end